﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CNVP.Framework.Utils;
using CNVP.JShop.UI;

namespace CNVP.JShop.Web.Member
{
    public partial class MemberPwd : UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region js验证
                txtOldPwd.Attributes.Add("onFocus", "RegTipInfo('lbOldPwd','请输入旧密码．')");
                txtOldPwd.Attributes.Add("onBlur", "return RegCheck('txtOldPwd','lbOldPwd')");
                txtPwd1.Attributes.Add("onFocus", "RegTipInfo('lbPwd1','请输入密码．')");
                txtPwd1.Attributes.Add("onBlur", "return RegCheck('txtPwd1','lbPwd1')");
                txtPwd2.Attributes.Add("onFocus", "RegTipInfo('lbPwd2','请输入确认密码，与上面一致．')");
                txtPwd2.Attributes.Add("onBlur", "return RegCheck('txtPwd2','lbPwd2')");
                btUp.Attributes.Add("onClick", "return CheckPwd()");
                #endregion
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            try
            {
                string OldPwd = this.txtOldPwd.Text.Trim();
                string UserPass1 = txtPwd1.Text.Trim();
                string UserPass2 = txtPwd2.Text.Trim();
                if (string.IsNullOrEmpty(OldPwd))
                {
                    MessageUtils.ShowPre("旧密码不能为空！");
                    return;
                }
                if ((UserPass1.Length < 6 || UserPass1.Length > 20) || string.IsNullOrEmpty(UserPass1))
                {
                    MessageUtils.ShowPre("新密码不能为空或格式不正确！");
                    return;
                }
                if (UserPass2.Length < 6 || UserPass2.Length > 20 || string.IsNullOrEmpty(UserPass2))
                {
                    MessageUtils.ShowPre("确认密码不能为空或格式不正确！");
                    return;
                }
                if (UserPass2 != UserPass1)
                {
                    MessageUtils.ShowPre("新密码和确认密码不相等！");
                    return;
                }

                if (new BLL.CNVP_Users().IsOldPwd(EncryptUtils.MD5(OldPwd), this.CookieString) > 0)
                {
                    new BLL.CNVP_Users().UpdatePwd(EncryptUtils.MD5(UserPass1), this.CookieString);
                    MessageUtils.ShowPre("密码修改成功！");
                }
                else
                {
                    MessageUtils.ShowPre("旧密码不存在！");
                    return;
                }
            }
            catch
            {
                MessageUtils.ShowPre("悲剧，出错了！");
            }

        }
    }
}